package com.pig4cloud.pig.gateway.config;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.pig4cloud.pig.gateway.filter.*;
import com.pig4cloud.pig.gateway.handler.GlobalExceptionHandler;
import com.pig4cloud.pig.gateway.handler.ImageCodeHandler;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
import org.springframework.data.redis.core.RedisTemplate;

/**
 * 网关配置
 * 日志追踪信息，ApiLoggingFilter设置，PigRequestGlobalFilter清除（后面搞个专门的过滤器清理）
 * @author L.cm
 */
@Configuration(proxyBeanMethods = false)
@EnableConfigurationProperties(GatewayConfigProperties.class)
public class GatewayConfiguration {
	/**
	 *  存储一些请求变量（ip、申请地址、token等）
	 */
	public static final String CONTEXT_MAP = "CONTEXT_MAP";

	@Bean
	public EncryptionTokenDecoderFilter encryptionTokenDecoderFilter(GatewayConfigProperties configProperties) {
		return new EncryptionTokenDecoderFilter(configProperties);
	}

	@Bean
	public ApiLoggingFilter apiLoggingFilter() {
		return new ApiLoggingFilter();
	}


	@Bean
	public PasswordDecoderFilter passwordDecoderFilter(GatewayConfigProperties configProperties) {
		return new PasswordDecoderFilter(configProperties);
	}


	@Bean
	public PigRequestGlobalFilter pigRequestGlobalFilter(GatewayConfigProperties configProperties) {
		return new PigRequestGlobalFilter(configProperties);
	}


	@Bean
	public ValidateCodeGatewayFilter validateCodeGatewayFilter(GatewayConfigProperties configProperties,
			ObjectMapper objectMapper, RedisTemplate redisTemplate) {
		return new ValidateCodeGatewayFilter(configProperties, objectMapper, redisTemplate);
	}

	@Bean
	public GlobalExceptionHandler globalExceptionHandler(ObjectMapper objectMapper) {
		return new GlobalExceptionHandler(objectMapper);
	}

	@Bean
	public ImageCodeHandler imageCodeHandler(RedisTemplate redisTemplate) {
		return new ImageCodeHandler(redisTemplate);
	}

}
